{{>licenseInfo}}
import expect from 'expect.js';
import { join } from 'path';

const modulePath = join(process.cwd(), 'src', '{{invokerPackage}}', 'index.js');
const {{moduleName}} = await import(modulePath);

let instance;

beforeEach(() => {
{{#models}}
{{#model}}
{{^isEnum}}
  try {
    instance = new {{moduleName}}.{{classname}}();
  } catch (e) {
    //Handle the cases when this model extends another model by using Model.call(this);
    instance = Object.create({{moduleName}}.{{classname}}.prototype);
  }
{{/isEnum}}
{{/model}}
{{/models}}
});

afterEach(() => {
  instance = null;
});

{{#models}}
{{#model}}
{{^isEnum}}
describe('{{classname}}', () => {
  it('should create an instance of {{classname}}', () => {
    expect(instance).to.be.a({{moduleName}}.{{classname}});
  });
{{/isEnum}}
{{#isEnum}}
// it's Enum type, no test needed.
describe('{{classname}}', () => {
{{/isEnum}}

{{#vars}}
  it('should have the property {{name}}', () => {
    // verify property exists
    expect(instance).to.have.property('{{name}}');

    // set and verify value
    {{#isEnum}}
    const expectedValue = [{{#allowableValues}}{{#values}}'{{{.}}}'{{^@last}}, {{/@last}}{{/values}}{{/allowableValues}}][0];
    {{/isEnum}}
    {{^isEnum}}
    const expectedValue = {{#isBodyParam}}generateMockData('{{dataType}}'{{#isArray}}, true{{/isArray}}){{/isBodyParam}}{{^isBodyParam}}{{#isArray}}generateMockData('{{items.dataType}}', true){{/isArray}}{{^isArray}}generateMockData('{{dataType}}'){{/isArray}}{{/isBodyParam}};
    {{/isEnum}}
    instance.{{name}} = expectedValue;
    expect(instance.{{name}}).to.equal(expectedValue);
  });

{{/vars}}
{{/model}}
{{/models}}
});

// Helper function to generate random test data
function generateMockData(dataType, isArray = false) {
  if (!dataType) return {};

  // Handle array types
  if (isArray) {
    return [generateMockData(dataType), generateMockData(dataType)];
  }

  switch(dataType) {
    case 'String':
      return 'mock-' + Math.random().toString(36).substring(2, 10);
    case 'Number':
      return Math.floor(Math.random() * 1000);
    case 'Boolean':
      return Math.random() > 0.5;
    case 'Date':
      return new Date().toISOString();
    default:
      try {
        const ModelClass = {{moduleName}}[dataType];
        if (ModelClass) {
          const instance = Object.create(ModelClass.prototype);
          return instance;
        }
      } catch (e) {
        console.error("Error creating instance of", dataType);
        return {};
      }
      return {};
  }
}
